#include "StdAfx.h"
#include "IconOverlayBase.h"

#include "SplitPath.h"
#include "shlwapi.h"

#include <string.h>

#include "ProcessListTester.h"
#include "StringFunctions.h"
#include "Md5Hasher.h"
#include "TcpClient.h"
#include "RegistryAccess.h"

CIconOverlayBase::CIconOverlayBase(void)
{
}

CIconOverlayBase::~CIconOverlayBase(void)
{
}

STDMETHODIMP CIconOverlayBase::GetOverlayInfo(
             LPWSTR pwszIconFile,
             int cchMax,
             int* pIndex,
             DWORD* pdwFlags)
{

// not required to get icon info as all work will be done by TortoiseOverlays
	fileList = "";
	fileListFolder="";
	usedFileList="";
  return S_OK;
}

// IShellIconOverlayIdentifier::GetPriority

// returns the priority of this overlay 0 being the highest. 

STDMETHODIMP CIconOverlayBase::GetPriority(int* pPriority)
{
  // we want highest priority 
// not required as all work will be done by TortoiseOverlays
  *pPriority=0;
  return S_OK;
}

// IShellIconOverlayIdentifier::IsMemberOf

// Returns whether the object should have this overlay or not 

STDMETHODIMP CIconOverlayBase::IsMemberOf(LPCWSTR pwszPath, DWORD dwAttrib)
{
  wchar_t *s = _wcsdup(pwszPath);
  HRESULT r = S_FALSE;
  
  // Criteria

	CComBSTR path(s);
	CSplitPath sp (path);

	CComBSTR folder (sp.GetDrive());
	folder.Append(sp.GetDirectory());

	CComBSTR fn ("|");
	fn.Append(sp.GetFileName());
	fn.Append(sp.GetExtension());
	fn.Append("|");

	if (fileList=="" || !(fileListFolder==folder) || (wcsstr(usedFileList.m_str, fn.m_str)!=0))
	{
		usedFileList="";
		string p = CComBSTRtoString( folder );

		areOverlaysApplicable = IsMonitorLizardProcessRunning();

		if (!areOverlaysApplicable)
			return S_FALSE;

		CComBSTR regKey="Software\\LizardTF";
		CComBSTR valueName = "MetaDataFolder";

		CComBSTR ltfFolder = GetRegistryKeyValue(regKey, valueName).m_str;
		ltfFolder.Append("\\");

		ltfFolder.Append(  metafolderpath(folder).c_str() );

		areOverlaysApplicable &= (PathFileExists(ltfFolder.m_str))!=0;



		//string status=cLizard.GetLizardStatus(p);

		//areOverlaysApplicable &= (status=="TRUE");

		if (!areOverlaysApplicable)
			return r;


		fileList = GetIconStatus(GetOverlayTypeName(), p).c_str();
		fileListFolder = folder.m_str;
	}

	if (!areOverlaysApplicable)
		return r;


	if (wcsstr(fileList.m_str, fn.m_str)!=0)
    r = S_OK;

	usedFileList.Append(fn.m_str);
	usedFileList.Append("|");

  free(s);

  return r;
}



string CIconOverlayBase::GetOverlayTypeName()
{
	string s = "";
	return s;
}